/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jpa;

import entity.Komentarbarang;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entity.Barang;
import javax.persistence.Persistence;
import jpa.exceptions.NonexistentEntityException;

/**
 *
 * @author has
 */
public class KomentarbarangJpaController implements Serializable {

    public KomentarbarangJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public KomentarbarangJpaController() {
        emf = Persistence.createEntityManagerFactory("RBPLPU");
    }

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Komentarbarang komentarbarang) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Barang idBarang = komentarbarang.getIdBarang();
            if (idBarang != null) {
                idBarang = em.getReference(idBarang.getClass(), idBarang.getIdBarang());
                komentarbarang.setIdBarang(idBarang);
            }
            em.persist(komentarbarang);
            if (idBarang != null) {
                idBarang.getKomentarbarangCollection().add(komentarbarang);
                idBarang = em.merge(idBarang);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Komentarbarang komentarbarang) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Komentarbarang persistentKomentarbarang = em.find(Komentarbarang.class, komentarbarang.getIdKomentarBarang());
            Barang idBarangOld = persistentKomentarbarang.getIdBarang();
            Barang idBarangNew = komentarbarang.getIdBarang();
            if (idBarangNew != null) {
                idBarangNew = em.getReference(idBarangNew.getClass(), idBarangNew.getIdBarang());
                komentarbarang.setIdBarang(idBarangNew);
            }
            komentarbarang = em.merge(komentarbarang);
            if (idBarangOld != null && !idBarangOld.equals(idBarangNew)) {
                idBarangOld.getKomentarbarangCollection().remove(komentarbarang);
                idBarangOld = em.merge(idBarangOld);
            }
            if (idBarangNew != null && !idBarangNew.equals(idBarangOld)) {
                idBarangNew.getKomentarbarangCollection().add(komentarbarang);
                idBarangNew = em.merge(idBarangNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = komentarbarang.getIdKomentarBarang();
                if (findKomentarbarang(id) == null) {
                    throw new NonexistentEntityException("The komentarbarang with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Komentarbarang komentarbarang;
            try {
                komentarbarang = em.getReference(Komentarbarang.class, id);
                komentarbarang.getIdKomentarBarang();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The komentarbarang with id " + id + " no longer exists.", enfe);
            }
            Barang idBarang = komentarbarang.getIdBarang();
            if (idBarang != null) {
                idBarang.getKomentarbarangCollection().remove(komentarbarang);
                idBarang = em.merge(idBarang);
            }
            em.remove(komentarbarang);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Komentarbarang> findKomentarbarangEntities() {
        return findKomentarbarangEntities(true, -1, -1);
    }

    public List<Komentarbarang> findKomentarbarangEntities(int maxResults, int firstResult) {
        return findKomentarbarangEntities(false, maxResults, firstResult);
    }

    private List<Komentarbarang> findKomentarbarangEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Komentarbarang.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Komentarbarang findKomentarbarang(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Komentarbarang.class, id);
        } finally {
            em.close();
        }
    }

    public int getKomentarbarangCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Komentarbarang> rt = cq.from(Komentarbarang.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
